(library (lib string-utils)
  (export string-repeat
          string-indent
          string-remove-suffixes)
  (import (rnrs base)
          (only (guile)
                lambda* λ
                current-output-port
                display
                call-with-output-string
                ;; string procedures
                string-suffix?
                string-drop-right)
          (ice-9 textual-ports)
          (srfi srfi-1)))


(define rest cdr)


(define string-repeat
  (λ (str n)
    (call-with-output-string
      (λ (port)
        (let iter ([repetitions n])
          (cond
           [(= repetitions 0)
            (put-string port "")]
           [else
            (put-string port str)
            (iter (- repetitions 1))]))))))


(define string-indent
  (lambda* (string indent-level #:key (indentation-width 8))
    (let ([one-indentation
           (string-repeat " " indentation-width)])
      (string-append (string-repeat one-indentation indent-level)
                     string))))


(define string-remove-suffixes
  (λ (str lst-of-suffixes)
    "Removes all suffixes in LST-OF-SUFFIXES in order from
left to right from the original string STR. Removes at
maximum one of the suffixes, as the others would not be true
suffixes of the original string, but the string created by
removing one of the other suffixes."
    (cond
     [(null? lst-of-suffixes)
      str]
     [(string-suffix? (first lst-of-suffixes) str)
      (string-drop-right str
                         (string-length (first lst-of-suffixes)))]
     [else
      (string-remove-suffixes str (rest lst-of-suffixes))])))
